package com.example.excel.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;

import com.example.excel.R;
import com.example.excel.base.BaseActivity;
import com.example.excel.bean.InventoryBean;
import com.example.excel.config.Constants;
import com.example.excel.databinding.ActivityMainBinding;
import com.example.excel.db.InventoryController;
import com.example.excel.ui.activity.local.LocalInfoActivity;
import com.example.excel.ui.activity.login.LoginActivity;
import com.example.excel.ui.activity.query.InventoryActivity;
import com.example.excel.ui.activity.setting.SettingActivity;
import com.example.excel.utils.SharedPreferencesUtils;
import com.example.excel.utils.Utils;

import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {

    public static void start(Activity context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
        context.finish();
    }

    @Override
    protected void initBundle(Bundle savedInstanceState) {
        super.initBundle(savedInstanceState);
        String token = SharedPreferencesUtils.getString(Constants.LOGIN_TOKEN, "");
        if (TextUtils.isEmpty(token)) {
            LoginActivity.startFinish(this);
        }
    }

    @Override
    protected Class<MainViewModel> initViewModel() {
        return MainViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mBinding.setEvent(new DataBindingMainHandler());
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.mShowUI.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Utils.getInstance().showToast(s);
            }
        });
        mViewModel.mLoadSuccess.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    new AlertDialog.Builder(MainActivity.this).setMessage("下载完成，共" + mViewModel.mTotal + "条数据").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
                }
            }
        });
        mViewModel.mUploadSuccess.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    new AlertDialog.Builder(MainActivity.this).setMessage("上传完成，共上传" + mViewModel.mTotal + "条数据").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
                }
            }
        });
    }

    private void showFinishDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定删除本地数据吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mViewModel.clear();
            }
        });
        builder.show();
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("本地有未提交的数据，确定删除本地数据吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mViewModel.clear();
            }
        });
        builder.show();
    }

    private void showDeleteDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("本地有未提交的数据，是否删除本地数据，重新下载？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mViewModel.clear();
            }
        });
        builder.show();
    }

    public class DataBindingMainHandler {
        // 资料下载
        public void onDownloadInfo(View view) {
            if (SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                showDeleteDialog();
                return;
            }
            mViewModel.loadInventoryData();
        }

        // 盘点
        public void onInventoryInfos(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            InventoryActivity.start(view.getContext());
        }

        // 上传数据
        public void onUploadInfo(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            if (SharedPreferencesUtils.getBoolean(Constants.UPLOAD_COMPLE, false)) {
                Utils.getInstance().showToast("盘点已完成，不能重复提交");
                return;
            }
            List<InventoryBean> beans = InventoryController.getInstance().queryNoInventoryData();
            if (beans != null && beans.size() > 0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("是否结束该盘点单？");
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mViewModel.uploadInventoryData();
                    }
                });
                builder.show();
                return;
            }
            mViewModel.uploadInventoryData();
        }

        // 查询
        public void onQueryInventory(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            LocalInfoActivity.start(view.getContext());
        }

        // 清除
        public void onClear(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            if (!SharedPreferencesUtils.getBoolean(Constants.UPLOAD_COMPLE, false)) {
                showDialog();
            } else {
                showFinishDialog();
            }
        }

        // 设置
        public void onSystemSettings(View view) {
            SettingActivity.start(view.getContext());
        }
    }
}
